package com.woniuxy.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.woniuxy.utils.AliasUtil;
@RestController
@ServerEndpoint("/webSocket/{u_id}")  

    public class WebSocketContrellor { 

        private static int onlineCount = 0; 

        private static Map<String, WebSocketContrellor> clients = new ConcurrentHashMap<String, WebSocketContrellor>(); 

        private Session session; 

        private String username; //用户别名

           

        @OnOpen ////连接成功建立的回调方法
        public void onOpen(@PathParam("u_id") int u_id, Session session) throws IOException { 
         //生成别名
        	String username=AliasUtil.alias();
            this.username = username; 
            this.session = session; 
            addOnlineCount(); //在线人数加1
            clients.put(username, this);//将客户放入Map中
             System.out.println(username);

        } 

       

        @OnClose 
        public void onClose() throws IOException { 
            clients.remove(username); //移除一用户
           subOnlineCount(); //在线人数减一
        } 

       

        @OnMessage //接收到消息的回调方法
        public void onMessage(String message) throws IOException {   
        	 Map<String, Object> mes = new ConcurrentHashMap<String, Object>();
             JSON.toJSON(message);
            Map maps = (Map)JSON.parse(message);
            Collection values = maps.values();
            Set keySet = maps.keySet();
            System.err.println(maps.get("price"));
            System.err.println(maps.get("operation"));
        
           if(maps.get("operation").equals("bidding")) {
        	     mes.put("price",maps.get("price"));
        	     mes.put("alias",maps.get("operation"));
              //判断获取的信息需要我们反馈给多少人
                 String jsonString = JSON.toJSONString(mes);
                 System.err.println(username+"竞了一次价");
                 sendMessageAll(jsonString); //反馈给所有人,当前价+1
              }                   
            } 

  

       

        @OnError //异常时执行
       public void onError(Session session, Throwable error) { 
        	System.err.println("message1");
            error.printStackTrace(); 
        } 

       

        public void sendMessageTo(String message, String To) throws IOException { //给某人回馈消息

            // session.getBasicRemote().sendText(message);
        	System.err.println("message2");
            //session.getAsyncRemote().sendText(message); 
            for (WebSocketContrellor item : clients.values()) { 
                if (item.username.equals(To) ) 
                    item.session.getAsyncRemote().sendText(message); 
            } 
        } 

           

        public void sendMessageAll(Object message) throws IOException { //给所有人反馈消息
        	System.err.println("message2");
            for (WebSocketContrellor item : clients.values()) { 
                item.session.getAsyncRemote().sendText(message.toString()); 
            } 
        } 

       

        public static synchronized int getOnlineCount() { 
            return onlineCount; 
        } 

       

        public static synchronized void addOnlineCount() { 
        	WebSocketContrellor.onlineCount++; 
        } 

       

        public static synchronized void subOnlineCount() { 
        	WebSocketContrellor.onlineCount--; 
        } 

       

        public static synchronized Map<String, WebSocketContrellor> getClients() { 
            return clients; 
        } 

}